﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Threading;
using System.Text;

namespace GDCommon.Mail
{
    public class MailHelper
    {
        public MailHelper()
        {
            //
            // TODO: Add constructor logic here
            //      
        }

        static MailHelper()
        {
            Thread t = new Thread(new ThreadStart(SendMail_WaitCallback));
            t.IsBackground = false;
            t.Start();
        }

        private static List<string> _dic = new List<string>();

        private static object state = new object();

        public static void SendMail(string email)
        {
            if (!string.IsNullOrEmpty(email))
            {
                _dic.Add(email);
            }
        }

        private static void SendMail_WaitCallback()
        {
            while (true)
            {

                string[] m_dic;

                lock (state)
                {
                    m_dic = _dic.ToArray();
                    _dic.Clear();
                }

                if (m_dic.Length == 0)
                {
                    //  sleep for this time
                    Thread.Sleep(3000);
                }
                else
                {
                    foreach (string item in m_dic)
                    {
    
                        try
                        {
                            MailBLL.SendMail(item);
                        }
                        catch (Exception e)
                        {

                        }
                    }
                }
            }
        }

        private static string CreateAuthStr(int len)
        {
            int number;
            StringBuilder checkCode = new StringBuilder();

            Random random = new Random();

            for (int i = 0; i < len; i++)
            {
                number = random.Next();

                if (number % 2 == 0)
                {
                    checkCode.Append((char)('0' + (char)(number % 10)));
                }
                else
                {
                    checkCode.Append((char)('A' + (char)(number % 26)));
                }

            }
            return checkCode.ToString();
        }

    }
}


